package com.group9.handinhand.controller.form;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.group9.handinhand.service.CommunityService;
import com.group9.handinhand.service.so.CommunitySO;
import com.group9.handinhand.service.so.EventSO;
import com.group9.handinhand.service.so.OfferSO;
import com.group9.handinhand.service.so.PostSO;
import com.group9.handinhand.service.so.RequestSO;
import com.group9.handinhand.service.so.UserSO;

public class UserWallViewForm {
	
	UserSO user;
	ArrayList<Object> userWallElements = new ArrayList<Object>();
	CommunityService communityService;
	/**
	 * A set method.
	 * Sets the elements on the community wall to the community.
	 * 
	 */
	public void setUserWallElements(){
		List<CommunitySO> communityList = communityService.getCommunitiesByUser(user);
		for(CommunitySO community: communityList){
			userWallElements.addAll(community.getEvents());
			userWallElements.addAll(community.getRequests());
			userWallElements.addAll(community.getOffers());
			userWallElements.addAll(community.getPosts());
			Collections.sort(userWallElements, new Comparator<Object>() {
				public int compare(Object o1, Object o2) {
					Date o1createDate = (o1 instanceof EventSO)?((EventSO)o1).getCreateDate(): (
							(o1 instanceof RequestSO)?((RequestSO)o1).getCreateDate():(
									(o1 instanceof OfferSO)?((OfferSO)o1).getCreateDate():(
											(o1 instanceof PostSO)?((PostSO)o1).getCreateDate():null)
									)
							);

					Date o2createDate = (o2 instanceof EventSO)?((EventSO)o2).getCreateDate(): (
							(o2 instanceof RequestSO)?((RequestSO)o2).getCreateDate():(
									(o2 instanceof OfferSO)?((OfferSO)o2).getCreateDate():(
											(o2 instanceof PostSO)?((PostSO)o2).getCreateDate():null)
									)
							);
					if (o1createDate == null || o2createDate == null)
						return 0;
					return o2createDate.compareTo(o1createDate);
				}
			});
		}
	}
	
	public UserSO getUser() {
		return user;
	}

	public void setUser(UserSO user) {
		this.user = user;
	}

	public CommunityService getCommunityService() {
		return communityService;
	}
	/**
	 * Sets the service to the community.
	 * @param communityService
	 */
	public void setCommunityService(CommunityService communityService) {
		this.communityService = communityService;
	}

	public void setUserWallElements(ArrayList<Object> userWallElements) {
		this.userWallElements = userWallElements;
	}

	public ArrayList<Object> getUserWallElements(){
		return userWallElements;
	}

}
